/**
 * Project Name:HomeActivity
 * Date:2016-7-4下午3:31:28
 * Copyright (c) 2016, askzhao All Rights Reserved.
 *
 */
package com.kkd.kkdapp.adapter;

import java.util.ArrayList;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.KKDWebActivity;
import com.kkd.kkdapp.activity.LoginActivity;
import com.kkd.kkdapp.activity.MyActivity;
import com.kkd.kkdapp.activity.MyRecommendFriendActivity;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.fragment.IndexFragment;
import com.kkd.kkdapp.http.entity.InviteFriendShare;
import com.kkd.kkdapp.http.entity.QuickIn;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.listener.OnSingleClickListener;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * ClassName: QuickEntryAdapter <br/>
 * date: 2016-7-4 下午3:31:28 <br/>
 * 
 * @author askzhao
 * @version
 */
public class QuickEntryAdapter extends
		RecyclerView.Adapter<QuickEntryAdapter.MyViewHolder> {

	private Context mContext;
	private ArrayList<QuickIn> mData;
	private LayoutInflater mInflater;
	private ArrayList<MyViewHolder> mHolderList = new ArrayList<QuickEntryAdapter.MyViewHolder>();

	/**
	 * @param mCurActivity
	 * @param mQuickIn
	 */
	public QuickEntryAdapter(Context context, ArrayList<QuickIn> mQuickIn) {
		mContext = context;
		mData = mQuickIn;
		mInflater = LayoutInflater.from(context);
	}

	public class MyViewHolder extends ViewHolder {
		TextView title;
		TextView number_tv;
		TextView desc;
		TextView new_tv;
		ImageView image;

		public MyViewHolder(View view) {
			super(view);
			image = (ImageView) view.findViewById(R.id.image);
			title = (TextView) view.findViewById(R.id.title);
			desc = (TextView) view.findViewById(R.id.desc);
			number_tv = (TextView) view.findViewById(R.id.number_tv);
			new_tv = (TextView) view.findViewById(R.id.new_tv);
		}
	}

	@Override
	public int getItemCount() {
		return mData.size();
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public void onBindViewHolder(MyViewHolder nViewHolder, final int pos) {
		if (!mHolderList.contains(nViewHolder)) {
			mHolderList.add(pos, nViewHolder);
		} else {
			mHolderList.remove(nViewHolder);
			mHolderList.add(pos, nViewHolder);
		}
		int line1Size = mData.size() % 2 == 0 ? mData.size() / 2
				: mData.size() / 2 + 1;
		final int position = pos % 2 == 0 ? pos / 2 : pos / 2 + line1Size;
		if (nViewHolder.image.getTag() == null
				|| !nViewHolder.image.getTag().toString()
						.equals(mData.get(position).getImageUrl())) {

			ImageLoader.getInstance().displayImage(
					mData.get(position).getImageUrl(), nViewHolder.image);
			nViewHolder.image.setTag(mData.get(position).getImageUrl());

		}
		String title = mData.get(position).getTitle();
		if (TextUtils.isEmpty(title)) {
			nViewHolder.title.setVisibility(View.GONE);
		} else {
			nViewHolder.title.setVisibility(View.VISIBLE);
			nViewHolder.title.setText(title);
		}
		String desc = mData.get(position).getDesc();
		if (TextUtils.isEmpty(desc)) {
			nViewHolder.desc.setVisibility(View.GONE);
		} else {
			nViewHolder.desc.setVisibility(View.VISIBLE);
			nViewHolder.desc.setText(desc);
		}

		int dp8 = SystemUtils.dip2px(mContext, 8);
		int dp13 = SystemUtils.dip2px(mContext, 13);
		boolean isShow = true;
		if (!title.equals("摇红包")) {// 摇红包不处理
			for (String qe : IndexFragment.mQuickEntryShow) {
				if (qe.equals(title)) {
					isShow = false;
					break;
				}
			}
		}

		if (title.equals("K计划")) {// K计划特殊处理
			if (PreferenceUtils.getInstance().getOneFinancialHotDot(mContext)) {
				isShow = true;
			} else {
				isShow = false;
			}

		}
		if (mData.get(position).getIsNew() == 1) {
			nViewHolder.number_tv.setVisibility(View.GONE);
			nViewHolder.new_tv.setVisibility(View.VISIBLE);

		} else {
			nViewHolder.new_tv.setVisibility(View.GONE);
			if (isShow) {
				if (mData.get(position).getNumber() < 0) {
					nViewHolder.number_tv.setVisibility(View.GONE);
				} else if (mData.get(position).getNumber() == 0) {
					nViewHolder.number_tv.setVisibility(View.VISIBLE);
					nViewHolder.number_tv.setText("");
					LayoutParams param = nViewHolder.number_tv
							.getLayoutParams();
					param.width = dp8;
					param.height = dp8;
					nViewHolder.number_tv.setLayoutParams(param);
				} else if (mData.get(position).getNumber() < 10) {
					nViewHolder.number_tv.setVisibility(View.VISIBLE);
					nViewHolder.number_tv.setText(mData.get(position)
							.getNumber() + "");
					LayoutParams param = nViewHolder.number_tv
							.getLayoutParams();
					param.width = dp13;
					param.height = dp13;
					nViewHolder.number_tv.setLayoutParams(param);
				} else {
					nViewHolder.number_tv.setVisibility(View.VISIBLE);
					nViewHolder.number_tv.setText("9+");
					LayoutParams param = nViewHolder.number_tv
							.getLayoutParams();
					param.width = dp13;
					param.height = dp13;
					nViewHolder.number_tv.setLayoutParams(param);
				}
			} else {
				nViewHolder.number_tv.setVisibility(View.GONE);
			}
		}

		nViewHolder.itemView.setOnClickListener(new OnSingleClickListener() {

			@Override
			public void onSingleClick(View v) {
				final String jumpto = mData.get(position).getJumpTo();
				String title = mData.get(position).getTitle();

				boolean isContain = false;
				for (String i : IndexFragment.mQuickEntryShow) {
					if (i.equals(title)) {
						isContain = true;
						break;
					}
				}
				if (!isContain)
					IndexFragment.mQuickEntryShow.add(title);
				if (!TextUtils.isEmpty(mData.get(position).getVersionMsg())) {
					((BaseActivity) mContext).showToast(mData.get(position)
							.getVersionMsg());
				} else {
					if (!TextUtils.isEmpty(mData.get(position).getShareUrl())) {
						if (PreferenceUtils.getInstance().getLogin(mContext) == 0) {
							InviteFriendShare inviteFriendShare = PreferenceUtils
									.getInstance().getInviteFriend(mContext);
							if (inviteFriendShare != null) {
								((BaseActivity) mContext).toShareURL(mContext,
										mData.get(position).getShareUrl(), mData.get(position).getShareTitle(),
										mData.get(position).getShareFuTitle(),mData.get(position).getShareLogo());
							} else {
								((BaseActivity) mContext)
										.showToast("请检查您的网络设置");
							}
						} else {
							Intent intent = new Intent(mContext,
									LoginActivity.class);
							mContext.startActivity(intent);
						}

					} else {
						if (jumpto.indexOf("http") < 0) {
							try {
								ComponentName componet = new ComponentName(
										"com.kkd.kkdapp", jumpto);
								Intent intent = new Intent();
								intent.setComponent(componet);
								intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
								mContext.startActivity(intent);
							} catch (Exception e) {
								e.printStackTrace();
								// 保证不崩
								// ((BaseActivity)mContext).showToast("请更新APP体验");
							}

						} else {
							if (!TextUtils.isEmpty(jumpto)) {
								Intent intent = new Intent(mContext,
										KKDWebActivity.class);
								intent.putExtra("url", jumpto);
								mContext.startActivity(intent);
							}
						}
					}

				}

				ItemChanged(pos);
			}
		});
	}

	protected void ItemChanged(int pos) {
		MyViewHolder nViewHolder = mHolderList.get(pos);
		int line1Size = mData.size() % 2 == 0 ? mData.size() / 2
				: mData.size() / 2 + 1;
		final int position = pos % 2 == 0 ? pos / 2 : pos / 2 + line1Size;
		int dp8 = SystemUtils.dip2px(mContext, 8);
		int dp13 = SystemUtils.dip2px(mContext, 13);
		boolean isShow = true;

		String title = mData.get(position).getTitle();
		if (!title.equals("摇红包")) {// 摇红包不处理
			for (String qe : IndexFragment.mQuickEntryShow) {
				if (qe.equals(title)) {
					isShow = false;
					break;
				}
			}
		}

		if (title.equals("K计划")) {// K计划特殊处理
			PreferenceUtils.getInstance()
					.setOneFinancialHotDot(mContext, false);
			if (PreferenceUtils.getInstance().getOneFinancialHotDot(mContext)) {

				isShow = true;
			} else {
				isShow = false;
			}

		}

		if (mData.get(position).getIsNew() == 1) {
			nViewHolder.new_tv.setVisibility(View.GONE);
			KkdHttpMagager.getInstance().reportNewImg(
					mData.get(position).getImgId(), "index",
					new HttpCallBack() {

						@Override
						public void onSuccess(int statusCode,
								Object responseBody) {
							// TODO Auto-generated method stub

						}

						@Override
						public void onFailure(int statusCode, String desc) {
							// TODO Auto-generated method stub

						}
					});
		} else {
			if (isShow) {
				if (mData.get(position).getNumber() < 0) {
					nViewHolder.number_tv.setVisibility(View.GONE);
				} else if (mData.get(position).getNumber() == 0) {
					nViewHolder.number_tv.setVisibility(View.VISIBLE);
					nViewHolder.number_tv.setText("");
					LayoutParams param = nViewHolder.number_tv
							.getLayoutParams();
					param.width = dp8;
					param.height = dp8;
					nViewHolder.number_tv.setLayoutParams(param);
				} else if (mData.get(position).getNumber() < 10) {
					nViewHolder.number_tv.setVisibility(View.VISIBLE);
					nViewHolder.number_tv.setText(mData.get(position)
							.getNumber() + "");
					LayoutParams param = nViewHolder.number_tv
							.getLayoutParams();
					param.width = dp13;
					param.height = dp13;
					nViewHolder.number_tv.setLayoutParams(param);
				} else {
					nViewHolder.number_tv.setVisibility(View.VISIBLE);
					nViewHolder.number_tv.setText("9+");
					LayoutParams param = nViewHolder.number_tv
							.getLayoutParams();
					param.width = dp13;
					param.height = dp13;
					nViewHolder.number_tv.setLayoutParams(param);
				}
			} else {
				nViewHolder.number_tv.setVisibility(View.GONE);
			}
		}

	}

	@Override
	public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		MyViewHolder holder = new MyViewHolder(mInflater.inflate(
				R.layout.home_quick_in_item, parent, false));
		LayoutParams params = holder.itemView.getLayoutParams();
		int w = SystemUtils.getScreenWidth(mContext) / 3;
		params.width = w;
		holder.itemView.setLayoutParams(params);
		return holder;
	}
}